using System;
using System.Collections.Generic;
using System.Text;

namespace TuringMachine.StateAutomat
{
    //Delegate event that sends tape action
    public delegate void TapeActionEventHandler(object sender, EventArgs e);

    class Automat
    {
        private IList<State> statesList = null;
        private State currentState = null;

        public State CurrentState
        {
            get { return currentState; }
            set { currentState = value; }
        }
        private State startState = null;        

        public Automat()
        {
            statesList = new List<State>();
            currentState = null;
            startState = null;
                    
        }

       

        public State GetStartState()
        {
            return startState;
        }

        public bool IsStateInAutomat(State state)
        {
            bool _ret = false;
            foreach (State stateToCheck in statesList)
            {
                if (stateToCheck.Name == state.Name)
                {
                    _ret = true;
                }
            }
            return _ret;
        }

        public State GetStateInstance(State state)
        {
            State _ret = null;
            foreach (State stateToCheck in statesList)
            {
                if (stateToCheck.Name == state.Name)
                {
                    _ret = stateToCheck;
                    break;
                }
            }
            return _ret;
        }

        public void AddState(State state)
        {
            foreach (State stateInAutomat in statesList)
            {
                if (stateInAutomat.Name == state.Name)
                {
                    throw new Exception("State " + state.Name + " already exist in automat");
                }

                
            }
            if (state.Type == StateType.StartState)
            {
                startState = state;
            }
            statesList.Add(state);
        }
    }

   
}
